import os

from pywinauto import Application, mouse
from pywinauto.keyboard import send_keys

# send_keys("12345")

app=Application(backend="uia").connect(process=19652)

#定位窗口
win=app.window(title_re=".*Sublime Text.*")
win.wait("visible")

#在subText中输入文本 keys,pause with_spaces默认为False不保留空格,with_newlines,默认不保留换行
# win.type_keys(keys="123456",pause=1)
# win.type_keys(keys="123  456",with_spaces=False)
# win.type_keys(keys="123\n456",with_newlines=True)

#虚拟键盘
# win.type_keys("hello{SPACE}world")

#虚拟键码-ctrl+s
# win.type_keys("^s")

# win.type_keys("^a")
# win.type_keys("{BACKSPACE}")
# win.type_keys("{VK_DELETE}")

#合并使用
# win.type_keys("^a{VK_DELETE}")

#shift(+) 输出括号
# win.type_keys("+9hello{SPACE 5}world+0")
# win.type_keys("1{+}2=3")

# menu_bar = win.child_window(title="应用程序", auto_id="MenuBar",control_type="MenuBar")
# #选择保存文件File->Save
# # save=menu_bar.item_by_path(path="File->Save")
# # point=save.rectangle().mid_point()
# # mouse.move(coords=(point.x, point.y))
#
# #选择File->Reopen With Encoding --精确匹配
# reopen=menu_bar.item_by_path(path="File->Reopen with Encoding",exact=True)
# point=reopen.rectangle().mid_point()
# mouse.move(coords=(point.x, point.y))


for i in range(1,2):
    #输入内容
    win.type_keys(f"创建第{i}个文件")

    #触发保存弹窗
    #1.快捷键的方式
    # win.type_keys("^s")

    #2.通过选择菜单选项
    menu_bar=win.child_window(title="应用程序",control_type="MenuBar")
    menu_bar.item_by_path("File -> Save").click_input()

    #3.menu_select
    # win.menu_select("File -> Save")

    #切换到保存弹窗
    save_win=win.child_window(title="另存为",control_type="Window")
    #在保存弹窗上--输入文件名
    filename=f"D:\\file\\GUITest\\test_{i}.txt"
    save_win.child_window(title="文件名:",control_type="Edit").type_keys(filename)
    #在保存弹窗上--点击保存按钮
    save_win.child_window(title="保存(S)",control_type="Button").click_input()
    #验证文件是否成功保存
    assert os.path.exists(filename)
    #创建新文件
    win.type_keys("^n")


